package com.lin.csdn.dao.impl;

import com.lin.csdn.dao.intf.IUserDao;
import com.lin.csdn.dao.BaseDao;
import com.lin.csdn.model.po.*;
import com.lin.csdn.model.vo.BeFollowListVO;
import com.lin.csdn.model.vo.FollowListVO;
import com.lin.csdn.model.vo.InformationVO;
import com.lin.csdn.model.vo.UserVO;

import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao implements IUserDao{

    @Override
    public List<Object> login(String email){
        List<Object> list = new ArrayList<>();
        list.add(email);
        Class<User> cls=User.class;
        return find("SELECT * FROM user where email=?", list,cls);
    }

    @Override
    public List<Object> searchAll(int beginIndex,int pageSize){
        List<Object> list = new ArrayList<>();
        String sql="SELECT * FROM user limit "+beginIndex+","+pageSize;
        System.out.println(sql);
        Class<UserVO> cls=UserVO.class;
        return find(sql,list,cls);
    }

    @Override
    public int sign(String name,String email,String password){
        List<Object> list = new ArrayList<>();
        list.add(name);
        list.add(email);
        list.add(password);
        list.add("用户");
        list.add("否");
        return update("INSERT INTO user (name,email,password,identity,status) VALUES (?,?,?,?,?)",list);
    }

    @Override
    public String findName(int id){
        List<Object> list=new ArrayList<>();
        list.add(id);
        Object result=findSingle("SELECT name FROM user where id=?",list);
        return result.toString();
    }

    @Override
    public int findId(String name){
        List<Object> list=new ArrayList<>();
        list.add(name);
        Object result=findSingle("SELECT id FROM user where name=?",list);
        Number userid= (Number) result;//long不能直接强转int，需要先转number
        return userid.intValue();
    }

    @Override
    public int beFollow(int loginId){
        List<Object> list=new ArrayList<>();
        list.add(loginId);
        Object result=findSingle(" select count(*) from follow where befollowed = ?",list);
        Number beFollow=(Number) result;
        return beFollow.intValue();
    }

    @Override
    public int follow(int loginId){
        List<Object> list=new ArrayList<>();
        list.add(loginId);
        Object result=findSingle(" select count(*) from follow where follow = ?",list);
        Number follow=(Number) result;
        return follow.intValue();
    }

    public List<Object> beFollowName(int loginId){
        List<Object> list=new ArrayList<>();
        list.add(loginId);
        Class<FollowListVO> cls= FollowListVO.class;
        return find("SELECT follow FROM follow where befollowed=?",list,cls);
    }

    public List<Object> followName(int loginId){
        List<Object> list=new ArrayList<>();
        list.add(loginId);
        Class<BeFollowListVO> cls=BeFollowListVO.class;
        return find("SELECT befollowed FROM follow where follow=?",list,cls);
    }

    public void banUser(int userId){
        List<Object> list=new ArrayList<>();
        list.add("是");
        list.add(userId);
        update("UPDATE user SET status=? WHERE id=?",list);
    }

    public Object searchInformation(int userId){
        List<Object> list=new ArrayList<>();
        list.add(userId);
        Class<InformationVO> cls=InformationVO.class;
        return find("SELECT * FROM user where id=?",list,cls);
    }

    public void updateEmail(int userId,String email){
        List<Object> list=new ArrayList<>();
        list.add(email);
        list.add(userId);
        update("UPDATE user SET email=? WHERE id=?",list);
    }
    public void updateName(int userId,String name){
        List<Object> list=new ArrayList<>();
        list.add(name);
        list.add(userId);
        update("UPDATE user SET name=? WHERE id=?",list);
    }
}
